#include "graphsupport/solve/facilitylocation/RemoveUnusedFacilityHeuristic.h"

#include "accumulsupport/AccumulSupport.h"

#include "graphsupport/solve/facilitylocation/ChooseCheapestFacilityHeuristic.h"

bool RemoveUnusedFacilityHeuristic::solve(
	FacilitiesGraph & fgraph,
	std::vector< char > & isFacilityOpenVec,
	std::vector< int > & cityWitnessVec
)
{
	int citiesCount = fgraph.getCitiesCount();
	int facilitiesCount = fgraph.getFacilitiesCount();

	bool witnessesExist = cityWitnessVec.size() != citiesCount;
	for ( int city = 0; city < citiesCount && witnessesExist; city++ )
	{
		witnessesExist &= cityWitnessVec[city] > -1;
	}

	if ( !witnessesExist )
	{
		bool solved = ChooseCheapestFacilityHeuristic().solve(
			fgraph,
			isFacilityOpenVec,
			cityWitnessVec
		);
		if ( !solved )
		{
			return false;
		}
	}
	accumf::reinit( isFacilityOpenVec, fgraph.getFacilitiesCount(), false );

	for ( int city = 0; city < citiesCount; city++ )
	{
		int witness = cityWitnessVec[city];
		assert( witness > -1 && witness < facilitiesCount );
		isFacilityOpenVec[witness] = true;
	}

	return true;
}
